from include.mydb import Mydb

class myModel(Mydb):

    def __init__(self,dbName,tablename,field):
        super(myModel,self).__init__(dbName,tablename)
        self.field =field

    def save(self):
        pass

    def get_list(self,where=None):
        mysql = self.whereToSql(where)
        list = self.query_db(mysql)
        list_gen = self.myFormat(list)
        return list_gen

    def get_one(self,where):
        mysql = self.whereToSql(where)
        row = self.query_db(mysql,one=True)
        row = self.myFormatToDic(row)
        return row

    def add(self,data):
        cx = self.insert(data)
        if not cx:
            return 'error'
        else:
            return cx

    def whereToSql(self,where):
        sql = ' where status=3 '
        if where:
            if type(where).__name__=='dict':
                for k,v in where.items():
                    str = "and{}={} ".format(k,v)
                    sql +=str
            elif type(where).__name__=='str':
                sql +=where
        return sql


    def myFormat(self,list):
        for i in list:
            res = {}
            for i1,x in enumerate(i):
                res[self.field[i1]] = x
            yield res

    def myFormatToDic(self,row):
        res = {}
        for i1,x in enumerate(row):
            res[self.field[i1]] = x
        return res

